<?php

namespace app\admin\controller;

use app\common\model\AdminModel;
use think\facade\View;

class AdminController extends Base
{

    private $model;

    /**
     * 构造方法
     * @access public
     * @throws \Exception
     */
    public function __construct()
    {
        parent::__construct();
        $this->model = new AdminModel();
    }

    /**
     * 列表
     * @return array|string
     * @throws \Exception
     */
    public function index()
    {
        $pageSize = $this->request->param('pageSize/d', 20); //每页多少条
        $pageNumber = $this->request->param('pageNumber/d',1); //当前页
        $keyword = $this->request->param('keyword');
        $map = [];
        if (!empty($keyword)) {
            $map[] = ['nickname|username', 'like', '%' . $keyword . '%'];
        }

        $list = $this->model
            ->where($map)
            ->order('id','desc')
            ->paginate($pageSize, false, ['page' => $pageNumber]);
        $this->result($list, '操作成功', 1);
    }

    /**
     * 添加
     * @return string
     * @throws \Exception
     */
    public function add(){
        if($this->request->isAjax()){
            $data = $this->request->param();
            $this->validate($data,[
                'mobile|手机号'=>'require',
                'nickname|姓名'=>'require',
                'password|密码'=>'require',
            ]);
            $data['is_super'] = empty($data['is_super'])?0:$data['is_super'];
            $this->model->add($data);
            $this->success('操作成功');
        }
        return View::fetch();
    }

    /**
     * 添加
     * @return string
     * @throws \Exception
     */
    public function edit(){
        if($this->request->isAjax()){
            $data = $this->request->param();
            $this->validate($data,[
                'mobile|手机号'=>'require',
                'nickname|姓名'=>'require',
            ]);
            $this->model->edit($data);
            $this->success('操作成功');
        }
        $id = $this->request->param('id/d');
        $info = $this->model->where('id',$id)->find();
        View::assign('info',$info);
        return View::fetch();
    }

    /**
     * 账户信息
     * @return string
     * @throws \Exception
     */
    public function info(){
        $info = $this->model->where('id',$this->loginInfo['id'])->find();
        if($this->request->isAjax()){
            $data = $this->request->param();
            $this->validate($data,[
                'mobile|手机号'=>'require',
                'nickname|姓名'=>'require',
            ]);
            $data['id'] = $info['id'];
            $this->model->edit($data);
            $this->success('操作成功');
        }
        View::assign('info',$info);
        return View::fetch();
    }

    /**
     * 设置用户状态
     */
    public function set(){
        $id = $this->request->param('id/d');
        $field = $this->request->param('field');
        $status = $this->request->param('status');
        $this->model->where('id',$id)->update([$field=>$status]);
        $this->success('操作成功');
    }

    /**
     * 分组selectpage选择
     * @return \think\response\Json
     * @throws \Exception
     */
    public function selectpage()
    {
        $pageSize      = $this->request->param('pageSize/d', 20);              //每页多少条
        $pageNumber    = $this->request->param('pageNumber/d',1);                //当前页
        $params = $this->getSelectPage();
        $map = $params['query'];
        $total  = $this->model->where($map)->count();
        $list   = [];
        if ($total > 0) {
            $fields = $this->request->param('fields', true);
            $list   = $this->model->field($fields)
                ->where($map)
                ->order($params['order'])
                ->page($pageNumber, $pageSize)
                ->select();
        }
        $data['totalRow'] = $total;
        $data['list']     = $list;
        return json($data);
    }

    /**
     * 删除用户
     * @throws \Exception
     */
    public function del(){
        $id = $this->request->param('id/d');
        $this->model->del($id);
        $this->success('操作成功');
    }

}